package com.waji.local;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.waji.http.SendImage;
 

import android.content.Context;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Environment;

public class SaveImage {
	 public static void saveimage(Context context,int who, final byte[] data, Camera camera){
		System.out.println("保存照片到手机");
		int number = SharedPrefer.ReadNumber(context);
		number=number+1;
		String numstring = Integer.toString(number);
		 String fileName = who+".jpg";
		File sdRoot = Environment.getExternalStorageDirectory();
		String dir = "/Camera/"
				+ new SimpleDateFormat("yyyy—MM—dd").format(new Date())
						.toString() + "/" + numstring+"/";
		File mkDir = new File(sdRoot, dir);
		if (!mkDir.exists())
			mkDir.mkdirs();
		File pictureFile = new File(sdRoot, dir + fileName);
		if (!pictureFile.exists()) {
			try {
				pictureFile.createNewFile();
				Camera.Parameters parameters = camera.getParameters();
				Size size = parameters.getPreviewSize();
				YuvImage image = new YuvImage(data,
						parameters.getPreviewFormat(), size.width, size.height,
						null);
				FileOutputStream filecon = new FileOutputStream(pictureFile);
				image.compressToJpeg(
						new Rect(0, 0, image.getWidth(), image.getHeight()),
						90, filecon);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		 
	 }

	public static void saveServiceimage(Context context,int who, byte[] data,
			Camera camera) {
		System.out.println("保存照片到手机");
		int number = SharedPrefer.ReadNumber(context);
		String numstring = Integer.toString(number);
		
		
		 SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss ");
        Date curDate = new Date(System.currentTimeMillis());
		 String str = formatter.format(curDate);
		 String fileName = str+".jpg";
		 if(who==3){SendImage.path1=fileName;}
		 else	if(who==4){SendImage.path2=fileName;}
		File sdRoot = Environment.getExternalStorageDirectory();
		String dir = "/Camera/"
				+ new SimpleDateFormat("yyyy—MM—dd").format(new Date())
						.toString() + "/" + numstring+"/";
		File mkDir = new File(sdRoot, dir);
		
		if (!mkDir.exists())
			mkDir.mkdirs();
		File pictureFile = new File(sdRoot, dir + fileName);
		if (!pictureFile.exists()) {
			try {
				pictureFile.createNewFile();
				Camera.Parameters parameters = camera.getParameters();
				Size size = parameters.getPreviewSize();
				YuvImage image = new YuvImage(data,
						parameters.getPreviewFormat(), size.width, size.height,
						null);
				FileOutputStream filecon = new FileOutputStream(pictureFile);
				image.compressToJpeg(
						new Rect(0, 0, image.getWidth(), image.getHeight()),
						90, filecon);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(who==4){
		SendImage image = new SendImage();
		image.send(context,2);}

	 }
 

}
